package net.zjitc.summary.user;

import java.io.*;
import java.util.ArrayList;

/**
 * @Author admin
 * @Date 2019/4/16 14:16
 * 工具类的具体实现类，用文件方式
 * 字节流：一般用来操作图片或视频
 * 基本字节输入输出流 FileInputStream\ FileOutputStream
 * 字节缓冲流：BufferedInputStream\BufferedOutputStream
 * 效率最高的是：用字节缓冲流一次读写一个字节数组
 * 字符流：一般用来操作文本
 * 基本字节流：FileReader\FileWriter
 * 字符缓冲流：BufferedReader\BufferedWriter
 * 效率最高的是用字符缓冲流一次读写一行文本
 * 若出现乱码（编码问题），则需要用到转换流：InputStreamReader\OutputStramWriter
 */
public class UserDaoIO implements UserDao {

    @Override
    public void register(User user) {
        Writer out = null;
        BufferedWriter bw = null;
        try {
            out = new FileWriter("user.txt");
            bw = new BufferedWriter(out);
            String s = user.getName() + "-" + user.getPassword() + "-" + user.getEmail();
            bw.write(s);
            bw.newLine();
            bw.flush();
            System.out.println("注册成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public User login(String name, String password) {
        /*循环读取文件中所有行：
         * 一行文本读取出来后，分割好，
         * 比较用户名和密码是否一致
         * 若一致，封装成user对象，并返回*/
        Reader in = null;
        BufferedReader br = null;
        try {
            in = new FileReader("user.txt");
            br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split("-");
                if (s[0].equals(name) && s[1].equals(password)) {
                    User user = new User(s[0], s[1]);
                    return user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void selectAll() {
        Reader in = null;
        BufferedReader br = null;
        try {
            in = new FileReader("user.txt");
            br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void delete(String name) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        ArrayList<User> arrayList = new ArrayList<User>();
        try {
            br = new BufferedReader(new FileReader("user.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split("-");
                User user = new User(s[0], s[1], s[2]);
                arrayList.add(user);
            }
            boolean flag = false;
            for (int i = 0; i < arrayList.size(); i++) {
                if (arrayList.get(i).getName().equals(name)) {
                    flag = true;
                    User u = arrayList.get(i);
                    arrayList.remove(u);
                    bw=new BufferedWriter(new FileWriter("user.txt"));
                    for (int j = 0; j <arrayList.size() ; j++) {
                        User user=arrayList.get(j);
                        String s=user.getName()+"-"+user.getPassword()+"-"+user.getEmail();
                        bw.write(s);
                        bw.newLine();
                    }
                    bw.flush();
                    System.out.println("删除成功！");
                    break;
                }
            }
            if (!flag){
                System.out.println("您要删除的用户不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
